/*
 * Copyright (C) 2020-present, kinbug All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitee.kinbug.sniper.indicator;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.gitee.kinbug.sniper.common.entity.Bar;
import com.tictactec.ta.lib.Core;
import com.tictactec.ta.lib.MInteger;
import com.tictactec.ta.lib.RetCode;

import lombok.extern.slf4j.Slf4j;

/**
 * -描述
 * @ClassName: ATRIndicator
 * @author kinbug
 * @date 2020-12-17
 */
@Slf4j
public class ATRIndicator {
    /**
	 * -描述
	 * @Title getValue
	 * @param  参数
	 * @return void 返回类型
	 * @throws
	 */
    public static void getValue(List<Bar> coreList,final int period,final List<BigDecimal> callback) {
    	callback.clear();
    	double[] inHigh = new double[coreList.size()];
    	double[] inLow = new double[coreList.size()];
    	double[] inClose = new double[coreList.size()];
        double[] outReal = new double[coreList.size()];
        MInteger outBegIdx = new MInteger();
        MInteger outNBElement = new MInteger();

        for (int i = 0; i < coreList.size(); i++) {
        	inHigh[i] = coreList.get(i).getHigh().doubleValue();
        	inLow[i] = coreList.get(i).getLow().doubleValue();
        	inClose[i] = coreList.get(i).getClose().doubleValue();
        }
        Core c = new Core();
		RetCode retCode =c.atr(0, coreList.size()-1, inHigh, inLow, inClose, period, outBegIdx, outNBElement, outReal);
        if (retCode == RetCode.Success) {
        	for (int i = 0; i < coreList.size()-period; i++) {
            	callback.add(new BigDecimal(outReal[i]).setScale(2, RoundingMode.DOWN));
            }
		}else {
			log.error("计算ADX异常");
		}
	}
}
